package com.registration.controller;

import com.registration.Annotation.OperateLog;
import com.registration.bean.DepartmentBean;
import com.registration.bean.DoctorBean;
import com.registration.service.DepartmentService;
import com.registration.util.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 获取医院科室列表（一级科室）
     * 对应文档：GET /api/v1/hospital/{id}/department
     * 注：简化为无需医院ID，直接返回所有一级科室
     */
    @GetMapping("/hospital/{hospitalId}/department")
    @OperateLog("获取医院科室列表")
    public JsonResponse getDepartmentList(@PathVariable Integer hospitalId) {
        List<DepartmentBean> depts = departmentService.getFirstLevelDepts();

        return JsonResponse.success(depts);
    }

    /**
     * 获取科室详情
     * 对应文档：GET /api/v1/department/{id}
     */
    @GetMapping("/department/{deptId}")
    @OperateLog("获取科室详情")
    public JsonResponse getDepartmentDetail(@PathVariable Integer deptId) {
        DepartmentBean dept = departmentService.getDeptById(deptId);
        if (dept == null) {
            return JsonResponse.fail("科室不存在或已停用");
        }
        return JsonResponse.success(dept);
    }

    /**
     * 获取科室下的医生列表
     * 对应文档：GET /api/v1/department/{id}/doctor
     */
    @GetMapping("/department/{deptId}/doctor")
    @OperateLog("获取科室下的医生列表")
    public JsonResponse getDoctorsByDept(@PathVariable Integer deptId) {
        List<DoctorBean> doctors = departmentService.getDoctorsByDeptId(deptId); // 需在DepartmentService补充该方法
        return JsonResponse.success(doctors);
    }
    /**
     * 获取所有科室列表（含子科室）
     * 对应文档：GET /api/v1/departments
     */
    @GetMapping("/departments")
    @OperateLog("获取所有科室列表")
    public JsonResponse getAllDepartments() {
        List<DepartmentBean> deptList = departmentService.getAllDepartmentsWithHierarchy();
        return JsonResponse.success(deptList);
    }
}
